package main

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"log"
	"math/rand"
	"time"

	"github.com/go-ping/ping"
)

func main() {
	fmt.Println("白狐牌苏微服务器网络检查工具 v0.4.271")
	fmt.Println("qq2184195491")
	fmt.Println("LoadIng。。。")
	time.Sleep(time.Second * time.Duration(rand.Intn(10)))
	fmt.Println(Ping("homo.msst.xyz", time.Second))
	fmt.Println(Ping("cr.ms-rss.top", time.Second))
	fmt.Println("测试结束")
	time.Sleep(time.Hour)

}
func Ping(addr string, t time.Duration) bool {
	// ping.New()
	pinger, err := ping.NewPinger(addr)
	if err != nil {
		return false
	}
	// 设置ping的次数
	pinger.Count = 6
	pinger.Timeout = t

	/*
		设置pinger将发送的类型。
		false表示pinger将发送“未经授权”的UDP ping
		true表示pinger将发送“特权”原始ICMP ping
	*/
	pinger.SetPrivileged(true)
	// 运行pinger
	err = pinger.Run()
	if err != nil {
		log.Fatalf("err -> %v", err)
	}
	stats := pinger.Statistics()
	fmt.Println(
		"地址",
		addr,
		"平均延时",
		stats.AvgRtt,
		"，远程IP：",
		stats.IPAddr,
	)
	fmt.Printf("丢包率：%.2f\n", stats.PacketLoss)
	if stats.PacketLoss > 0.5 {
		logrus.Warnf("丢包率过高，可能影响您的体验，请尝试更换网络环境！")
	}
	return true
}
